{extend name="./app/admin/view/main.html" /}
{block name="title"}
{assign name="itemName" value="自动回复" /}
熊掌号自动回复 - {/block}
{block name="style"}
<style>
</style>
{/block}
{block name="main"}
<header class="mipcms-container-header">
    <div class="float-left header-group">
        <h4 class="title">熊掌号</h4> <h5 class="sub-title">自动回复</h5>
    </div>
   <button type="button" class="pull-right ivu-btn ivu-btn-primary ivu-btn-circle" @click='itemAdd'>
        <span><Icon type="plus-round"></Icon> 添加{$itemName}</span>
    </button>
</header>

<main class="mipcms-container-body" style="height: calc(100% - 50px)">
    <section class="mip-box">
        <section class="mip-box-body">
            <!--内容列表-->
            <section class="diy-table-list" v-cloak>
                <div class="diy-table-item diy-table-item-header">
                    <ul class="list-unstyled row">
                       
                        <li class="col-md-2">
                            <span>关键词</span>
                        </li>
                        <li class="col-md-3">
                            <span>回复内容</span>
                        </li>
                        <li class="col-md-2">
                            <span>添加时间</span>
                        </li>
                        <li class="col-md-2">
                            <span>操作</span>
                        </li>
                    </ul>
                </div>
                <div v-if='itemList.length' class="diy-table-body">
                    <div class="diy-table-item" v-for='item in itemList' >
                         <ul class="list-unstyled row">
                            <li class="col-md-2">
                                {{item.user_content}}
                            </li>
                            <li class="col-md-3 over-h-e">
                                {{item.reply_content}}
                            </li>
                            <li class="col-md-2 over-h-e">
                                <span>{{item.add_time | dateTime}}</span>
                            </li>
                            <li class="col-md-2">
                                <i-button type="error" size="small" @click='itemDel(item)'>删除</i-button>
                                <i-button type="primary" size="small" @click='itemEdit(item)'>修改</i-button>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="no-block" v-else>
                    <Spin fix v-if="loading">数据正在加载中...</Spin>
                    <Icon type="ios-filing-outline"></Icon>
                    <p v-if='settingStatus'>暂无数据</p>
                    <p v-else>尚未配置熊掌号开发者信息，请<a href="{$domain}/{$Think.config.admin}/xiongzhang/xiongZhangSetting/">前去配置</a></p>
                </div>
            </section>
            <!--内容列表结束-->
        </section>
    </section>
      <Modal :title="dialogItemTitle" v-model='dialogItemStatus' v-cloak>
            <i-form :model="item" :rules="itemRules" ref="item" :label-width="80" >
                <Form-Item label="关键词" prop="userContent">
                    <i-input v-model="item.userContent"></i-input>
                </Form-Item>                
                <Form-Item label="回复内容" prop="replyContent">
                    <i-input type="textarea" v-model="item.replyContent"></i-input>
                </Form-Item>
            </i-form>
            <div slot="footer" class="dialog-footer">
                <i-button @click="dialogItemStatus = false">取 消</i-button>
                <i-button type="primary" @click="itemPost('item')">确 定</i-button>
            </div>
        </Modal>
</main>
{/block}
    
{block name="javascript"}
{__block__}
<script>
    new Vue({
        el: '#app',
        data: {
            loading: false,
            itemName: '{$itemName}',
            itemList: [], 
            settingStatus: false,
            
            item: {
                id: '',
                userContent: '',
                replyContent: '',
            },
            dialogItemTitle: '添加{$itemName}',
            dialogItemStatus: false,
            
            itemRules: {
                userContent: [{
                    required: true,
                    message: '请输入关键词',
                    trigger: 'blur'
                }],
                replyContent: [{
                    required: true,
                    message: '请输入回复内容',
                    trigger: 'blur'
                }],
            },
            pagination: {
                currentPage: 1,
                limit: 10,
                total: this.total,
            },
        },
        watch: {
             
        },
        mounted() {
            this.getSettingData();
        },
        methods: {
            itemAdd() {
                this.item.id = '';
                this.dialogItemTitle = '添加{$itemName}';
                this.item.userContent = '';
                this.item.replyContent = '';
                this.dialogItemStatus = true;
            },
            getSettingData: function() {
                this.loading = true;
                this.$mip.ajax('{$domain}/xiongzhang/ApiAdminXiongZhangSetting/settingList', {

                }).then(res => {
                    this.loading = false;
                    if(res.code == 1) {
                        if (res.data.baiduXZClientId && res.data.baiduXZClientSecret) {
                            this.settingStatus = true;
                            this.getItemList();
                        } else {
                            this.settingStatus = false;
                        }
                    }
                });
            },
            getItemList() {
                this.loading = true;
                this.$mip.ajax('{$domain}/xiongzhang/ApiAdminXiongZhangAutoReply/itemList', {
                }).then(res => {
                    this.itemList = [];
                    if(res.code == 1) {
                        var itemList = res.data.itemList;
                        this.itemList = itemList;
                    }
                    this.loading = false;
                });
            },
            itemEdit(item) {
                this.item.id = item.id;
                this.dialogItemTitle = '修改{$itemName}';
                this.item.userContent = item.user_content;
                this.item.replyContent = item.reply_content;
                this.dialogItemStatus = true;
            },
            itemPost(val) {
                var _this = this;
                this.$refs[val].validate((valid) => {
                    if(valid) {
                        if (this.item.id) {
                            this.$mip.ajax('{$domain}/xiongzhang/ApiAdminXiongZhangAutoReply/itemEdit', {
                                id: this.item.id,
                                userContent: this.item.userContent,
                                replyContent: this.item.replyContent,
                            }).then(function(res) {
                                if(res.code == 1) {
                                    _this.$Message.success('修改成功');
                                    _this.getItemList();
                                    _this.dialogItemStatus = false;
                                }
                            });
                        } else {
                            this.$mip.ajax('{$domain}/xiongzhang/ApiAdminXiongZhangAutoReply/itemAdd', {
                                userContent: this.item.userContent,
                                replyContent: this.item.replyContent,
                            }).then(function(res) {
                                if(res.code == 1) {
                                    _this.$Message.success('添加成功');
                                    _this.getItemList();
                                    _this.dialogItemStatus = false;
                                }
                            });
                        }
                        
                    }
                });
            },
            itemPaginationSelect(val) {
                this.pagination.limit = val;
                this.getItemList();
            },
            itemPaginationClick(val) {
                this.pagination.currentPage = val;
                this.getItemList();
            },
             
        }
    })
  </script>
{/block}